import jieba as jieba
from wss.stopWords import stopwordslist

cut_all = False

def ModeSelectionSetting(ModeSelection:str):
    global cut_all
    if(ModeSelection == 'jzms'):
        cut_all = False
    if(ModeSelection == 'qms'):
        cut_all = True
    if(ModeSelection == 'ssyqms'):
        cut_all = None


def seg_depart_jieba(sentence:str, isDelStopWords:bool):
    if(cut_all == None):
        sentence_depart = jieba.lcut_for_search(sentence.strip())
    else:
        sentence_depart = jieba.lcut(sentence.strip(),cut_all = cut_all)
    stopwords = stopwordslist()
    outWords = []
    for word in sentence_depart:
        if isDelStopWords:
            if word not in stopwords:
                if word != '\t':
                    outWords.append(word)
        else:
            if word != '\t':
                outWords.append(word)
    return outWords


def seg_depart_Count_jieba(sentence:str, isDelStopWords:bool):
    if(cut_all == None):
        words = jieba.lcut_for_search(sentence.strip())
    else:
        words = jieba.lcut(sentence.strip(),cut_all = cut_all)
    stopwords = stopwordslist()
    counts = {}
    for word in words:
        if  len(word) == 1:
            continue
        else:
            if isDelStopWords:
                if word not in stopwords:
                    counts[word] = counts.get(word, 0) + 1  
            else:
                counts[word] = counts.get(word, 0) + 1  
    items = list(counts.items())
    items.sort(key=lambda x: x[1], reverse=True)
    return items
    
    # for item in items:
    #     word, count = item
    #     print("{0:<5}{1:>5}".format(word, count))